package cn.wolfcode.web.controller;

import cn.wolfcode.ann.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private  IPermissionService permissionService;
    public PermissionController(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }
    @RequiredPermission(name="权限页面",expression = "permission:list")
    @RequestMapping("/list")
    //Model 用来封装数据
    public String list(Model model, QueryObject qo) {
        //改成PageInfo封装
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }
    @RequiredPermission(name="权限加载",expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    //Model 用来封装数据
    public JsonResult reload() {
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  new JsonResult(false,"加载失败");
        }
    }


}
